(function(){
    function Minion(){
        var breadcrumb = new App.models.Breadcrumb({
            prefix: [
                {text: '主机管理', anchor: '/task'}
            ],
            curr: '节点列表'
        });

        var MainView = Backbone.View.extend({
            // el与initialize一样是bk的关键字, 不能重命名为其它
            el: '#page-cnt',
            template:  _.template($("#tpl-minion").html()),
            events: {},
            initialize: function() {
                // 创建面包屑视图
                this.breadcrumbView = new App.views.BreadcrumbView({
                    model: breadcrumb
                });
                this.render();
            },
            render: function(){
                this.$el.html(this.template());
                // 只有模板渲染后才能加载表格插件
                this.tableView = new App.views.BaseTableView({
                    el: '#minion-detail .bootstrap-table',
                    columns: [
                        {field: 'minionId',     title: '节点ID'},
                        {
                            field: 'hwareId',
                            title: '硬件序列号',
                            formatter: function(value, row, index){
                                return widthRestrictedPopover(value, value, 'wordcut-120');
                            }
                        },
                        {
                            field: 'kernel',
                            title: '内核版本',
                            formatter: function(value, row, index){
                                var text = value.split('-')[0];
                                return textTooltip(value, text);
                            }
                        },
                        {field: 'cpu',     title: 'cpu核心'},
                        {field: 'memory',     title: '内存'},
                        {field: 'os',     title: '系统类型'},
                        {
                            field: 'osrelease',
                            title: '系统版本',
                            formatter: function(value, row, index){
                                // 示例: 7.2.511, 只取7.2
                                // var pattern = /^\d+\.\d+/;
                                var pattern = new RegExp('^\\d+\\.\\d+');
                                var text = pattern.exec(value)[0];
                                return textTooltip(value, text);
                            }
                        },
                        {field: 'pyversion',     title: 'python版本'},
                        {field: 'saltpath',     title: 'salt路径', visible: false},
                        {field: 'saltversion',     title: 'salt版本'},
                        {field: 'osarch',     title: '系统架构'},
                        {field: 'cpuarch',     title: 'cpu架构', visible: false},
                        {
                            field: 'cpuModel',
                            title: 'cpu型号',
                            visible: false,
                            formatter: function(value, row, index){
                                var text = value.split('-')[0];
                                return textTooltip(value, text);
                            }
                        },
                        {field: 'virtual',     title: '虚拟化'},
                        {
                            field: 'mac',
                            title: 'mac地址',
                            formatter: function(value, row, index){
                                /*
                                 * value原始值大概为这种形式: "{u'eno16777736': u'00:0c:29:3a:91:0b'}"
                                 * 我们需要把那个'u'字符去掉, 然后把键值所在的单引号都变成双引号, 否则parse会出错.
                                 */
                                value = value.replace(/u'/g, "'").replace(/'/g, '"');
                                var _dict = JSON.parse(value);
                                delete _dict.lo;

                                var _list = [];
                                $.map(_dict, function(val, key){
                                    // 排除可能的虚拟网卡
                                    if(key.indexOf('virbr0') != -1) return;
                                    _list.push(key + ': ' + val);
                                });
                                if(_list.length == 1) return _list;
                                var first = _list[0];
                                var popText = _list.join('<br/>');
                                return collapsedPopover(popText, first);
                            }
                        },
                        {
                            field: 'ipv4',
                            title: 'IP',
                            formatter: function(value, row, index){
                                if(!value || value == '') return '';

                                var _list = [];
                                $.map(value.split(','), function(val, idx){
                                    if(val != '127.0.0.1') _list.push(val);
                                });
                                if(_list.length == 1) return _list;
                                var first = _list[0];
                                var popText = _list.join('<br/>');
                                return collapsedPopover(popText, first);
                            }
                        },
                        {field: 'disk',     title: '硬盘'},
                    ],
                    opts: {
                        url: App.apiList.minion.query,
                        toolbar: '#minion-detail .toolbar',
                    },
                    eventBus: this.eventBus
                });
            },
        });

        var mainView = new MainView();
        console.log('minion start');
    }

    App.modules.Minion = Minion;
})();